<?php

class PositionsController extends BaseController
{
	
	/**
	 * Returns the data model based on the primary key given in the GET variable.
	 * If the data model is not found, an HTTP exception will be raised.
	 * @param integer the ID of the model to be loaded
	 */
	public function loadModel($id)
	{
		$model=Positions::model()->findByPk($id);
		if($model===null)
			throw new CHttpException(404,'The requested page does not exist.');
		return $model;
	}

	/**
	 * Performs the AJAX validation.
	 * @param CModel the model to be validated
	 */
	protected function performAjaxValidation($model)
	{
		if(isset($_POST['ajax']) && $_POST['ajax']==='positions-form')
		{
			echo CActiveForm::validate($model);
			Yii::app()->end();
		}
	}

	/**
	 * 资讯推荐页面和推荐位
	 * @param 
	 */
	public function actionNewsRecommend()
	{
		$nav = new Nav('search');
		$nav->unsetAttributes();  // clear any default values
		$nav->zna_type = 3;//'类型：1商家中心，2会员中心，3前台页面'
		$page = 100;
		$provider=$nav->search($page);	
	
		$this->render('newsrecommend',array(
				'provider'=>$provider,
		));
	}

	/**
	 * 添加资讯推荐位页面
	 * @param 
	 */
	public function actionAddNewsPage()
	{
		$dataProvider=new CActiveDataProvider('Nav');
		$this->render('newsaddpage',array(
			'dataProvider'=>$dataProvider,
		));
	}
	
	/*
	 * 推荐位页面设置
	* */
	public function actionIfPosition(){
		$listData = $_POST['ifposition'];
		//print_r($_POST);exit;
		Nav::model()->setIfPosition($listData,2);
		$this->redirect($this->pre_module_url.'/positions/newsrecommend');
	}

	/**
	 * 删除产品推荐位页面
	 * @param 
	 */
	public function actionDeleteNewsPage()
	{
		$id = Yii::app()->request->getParam('id');
		$model    = Nav::model()->findByPk($id);
		$position = Positions::model()->getPositionsByPage($id,2);
		if($model && $position){
			$this->render('../index/error',array(
					'message'=>"删除失败，此页面有未删除的推荐位，请先删除。",
					'links'=>array(
							array('资讯推荐位管理',$this->pre_module_url.'/positions/newsrecommend'),
					),
			));
		}else{
			$model->unsetPositionPage($id,2);
			//成功跳转
			$this->redirect(array('newsrecommend'));
		}
	}

	/**
	 * 添加推荐位
	 * @param 
	 */
	public function actionAddNewsPosition()
	{
		$model = new Positions();
		if(isset($_POST['Positions'])){
			$model->attributes=$_POST['Positions'];
			$model->zpo_type=2;
			//print_r($model->attributes);exit;
			if($model->save()){
				//成功跳转
				$this->redirect(array('newsrecommend'));
			}
		}else{
			$model->zna_id = Yii::app()->request->getParam('id');
		} 
		$this->render('addnewsposition',array(
			'model'=>$model,
		));
	}

	/**
	 * 编辑推荐位
	 * @param 
	 */
	public function actionEditNewsPosition()
	{		
		$id = Yii::app()->request->getParam('id');
		$model = Positions::model()->findByPk($id,'zpo_type=2');
		if(isset($_POST['Positions'])){
			$model->attributes=$_POST['Positions'];
			//print_r($model->attributes);exit;
			if($model->save()){
				//成功跳转
				$this->redirect(array('newsrecommend'));
			}
		}
		$this->render('addnewsposition',array(
			'model'=>$model,
		));
	}

	/**
	 * 删除推荐位
	 * @param 
	 */
	public function actionDeleteNewsPosition()
	{		
		$id = Yii::app()->request->getParam('id');
		$model = Positions::model()->findByPk($id,'zpo_type=2');
		if(!$model){
			$this->render('../index/error',array(
					'message'=>"删除失败，没有对应的推荐位。",
					'links'=>array(
							array('资讯推荐位管理',$this->pre_module_url.'/positions/newsrecommend'),
					),
			));
		}elseif($model->data){
			$this->render('../index/error',array(
					'message'=>"删除失败，此推荐位有未删除数据，请先清空数据。",
					'links'=>array(
							array('资讯推荐位管理',$this->pre_module_url.'/positions/newsrecommend'),
					),
			));
		}else{
			$model->delete();
			//成功跳转
			$this->redirect(array('newsrecommend'));
		}
	}

	/**
	 * 推荐资讯
	 * @param 
	 */
	public function actionAddRecommendNews()
	{
		$model=new News('search');
		$model->unsetAttributes();  // clear any default values
		$model->zne_status=2;
		if(isset($_GET['News'])){
			$model->attributes=$_GET['News'];
		}
		
		$id = Yii::app()->request->getParam('zpoid');
		$position = Positions::model()->findByPk($id);
		$contents = PosLog::model()->findAll('zpo_id=:zpo_id',array(':zpo_id'=>$id));
		//print_r($contents);exit;
		$ids = array();
		foreach ($contents as $value){
			$ids[] = $value->zpl_content_id;
		}
		$this->render('addrecommendnews',array(
				'model'=>$model,
				'ids'=>$ids,
				'position'=>$position,
		));
	}

	/**
	 * AJAX推荐资讯
	 * @param 
	 */
	public function actionDoRecommendNewsAjax()
	{
		$zpoid = Yii::app()->request->getParam('zpoid');
		$zneid  = Yii::app()->request->getParam('zpid');
		$start = strtotime(Yii::app()->request->getParam('start'));
		$end   = strtotime(Yii::app()->request->getParam('end'));
		if($zpoid && $zneid){
			$check = PosLog::model()->checkForRecommend($zpoid,$zneid,2);
			if($check==1){
				if($start && $end && $start<$end){
					$news = News::model()->findByPk($zneid);
					$model   = new PosLog();
					$model->zpo_id         = $zpoid;
					$model->zpl_catid      = $news->znc_id;
					$model->zpl_title      = $news->zne_title;
					$model->zpl_pic        = $news->zne_thumb;
					$model->zpl_des        = $news->zne_desc;
					$model->zpl_content_id = $zneid;
					$model->zpl_start_time = $start;
					$model->zpl_end_time   = $end;
					$model->save() ? $code = 1 : $code = 7;
				}else{
					$code = 2;//'时间设置错误，请重试！';
				}
			}elseif($check==2){
				$code = 3;//'内容已满！';
			}elseif($check==3){
				$code = 4;//'内容已推荐！';
			}elseif($check==4){
				$code = 5;//'产品不存在或不可推荐！';
			}elseif($check==5){
				$code = 6;//'推荐位不存在！';
			}elseif($check==6){
				$code = 7;
			}			
		}else{
			$code = 8;//'参数错误，请重试';
		}
		echo json_encode(array('code'=>$code,'zpid'=>$zneid));
	}

	/**
	 * AJAX取消推荐
	 * @param 
	 */
	public function actionUnrecommendNewsAjax()
	{
		$zpoid = Yii::app()->request->getParam('zpoid');
		$zneid  = Yii::app()->request->getParam('zpid');
		$model = PosLog::model()->find('zpl_content_id='.$zneid.' AND zpo_id='.$zpoid);
		if($model && $model->delete()){
			echo json_encode(array('code'=>1,'zpid'=>$zneid));
		}else{
			echo json_encode(array('code'=>2,'zpid'=>$zneid));
		}		
	}

	/**
	 * 已推荐资讯列表
	 * @param 
	 */
	public function actionRecommendNews()
	{
		$position = Positions::model()->findByPk(Yii::app()->request->getParam('zpoid'));
		$model = PosLog::model();
		$this->render('recommendnews',array(
				'model'=>$model,
				'position'=>$position,
		));
	}
	
	/**
	 * 编辑推荐
	 */
	public function actionEditNewsRecommend($id)
	{
		if(isset($_POST['PosLog'])){
			$model = PosLog::model()->findByPk($_POST['PosLog']['zpl_id']);
			$model->attributes = $_POST['PosLog'];
			$model->zpl_start_time = strtotime($model->zpl_start_time);
			$model->zpl_end_time   = strtotime($model->zpl_end_time);
			$model->save();
			Yii::app()->request->redirect($this->pre_module_url.'/positions/recommendnews/zpoid/'.$model->zpo_id);
		}else{
			$id    = Yii::app()->request->getParam('id',0);
			$model = PosLog::model()->findByPk($id);
			$model->zpl_start_time = date('Y-m-d',$model->zpl_start_time);
			$model->zpl_end_time   = date('Y-m-d',$model->zpl_end_time);
			$this->render('editnewsrecommend',array(
					'model'=>$model,
			));
		}
	}
	
	/**
	 * 删除推荐产品
	 */
	public function actionDeleteNewsRecommend()
	{
		$ids = Yii::app()->request->getParam('ids',0);
		$idbox = array(0);
		if(is_array($ids) && count($ids)){
			foreach($ids as $key=>$id){
				(int)$id && $idbox[] = (int)$id;
			}
		}elseif($ids){
			(int)$ids && $idbox[] = (int)$ids;
		}else{
			$message = '删除失败，未选中任何选项。';
		}
		if(count($idbox)>1 && PosLog::model()->deleteRecommend($idbox, Yii::app()->user->userid)){
			$message = '删除成功。';
		}elseif(count($idbox)>1){
			$message = '删除失败。';
		}
		echo '<script>if(confirm("'.$message.'")){self.history.back();}</script>';
	}
	
}
